//
//  ViewController.swift
//  ios_mj_refresh
//
//  Created by GAOMO on 2019/11/14.
//  Copyright © 2019 GAOMO. All rights reserved.
//

import UIKit

import UIKit
import MJRefresh
import SnapKit
//class ViewController:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    var tableView: UITableView!
    
    var index = 0
    var data = [Int]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
       initView()
    }
    
      func initView(){
        var t = UILabel()
        t.text = "1"
        self.view.addSubview(t)
        t.snp.makeConstraints{
            (make) in
            make.top.equalTo(self.view.snp.top)
        }
        var t1 = UILabel()
        t1.text = "1"
        self.view.addSubview(t1)
        t1.snp.makeConstraints{
            (make) in
            make.top.equalTo(t.snp.bottom)
        }
        var t2 = UILabel()
        t2.text = "1"
        self.view.addSubview(t2)
        t2.snp.makeConstraints{
            (make) in
            make.top.equalTo(t1.snp.bottom)
        }
        var t3 = UILabel()
        t3.text="1"
        self.view.addSubview(t3)
        self.title = "RefreshLoadDemo"
               self.view.backgroundColor = .white
               
               tableView = UITableView(frame: self.view.frame)
               tableView.dataSource = self
               tableView.delegate = self
        
        //self.tableView.estimatedRowHeight = 180.0 //设置一个默认高度

     //   self.tableView.rowHeight=UITableView.automaticDimension  //开启自适应

        self.tableView.rowHeight=180
        tableView.setupHeaderFooterV(headerV: t3, footerV: nil)
               tableView.register(TableViewCell.self, forCellReuseIdentifier: "cell")
               self.view.addSubview(tableView)
               self.tableView.mj_header = RefreshView(refreshingBlock: {
                   [weak self] () -> Void in
                   self?.headerRefresh()
               })
               self.tableView.mj_footer = LoadMoreView(refreshingBlock: {
                   [weak self] () -> Void in
                   self?.footerRefresh()
               })
               
               // 默认刷新一下数据
               self.tableView.mj_header?.beginRefreshing()
        
        tableView.snp.makeConstraints{
            (make) in
            make.top.equalTo(t2.snp.bottom)
            make.left.equalTo(self.view.snp.left)
            make.right.equalTo(self.view.snp.right)
            make.bottom.equalTo(self.view.snp.bottom)
        }
        
    }
    
    @objc func headerRefresh() {
        self.data.removeAll()
        index = 0
        self.tableView.mj_footer?.resetNoMoreData()
        for _ in 0..<20 {
            data.append(Int(arc4random()))
        }
        Thread.sleep(forTimeInterval: 1)
        self.tableView.reloadData()
        self.tableView.mj_header?.endRefreshing()
    }
    
    @objc func footerRefresh() {
//        for _ in 0...2 {
//            self.tableView.mj_footer?.endRefreshingWithNoMoreData()
//        }
        
        for _ in 0..<20{
            data.append(Int(arc4random()))
        }
        Thread.sleep(forTimeInterval: 1)
        self.tableView.reloadData()
        self.tableView.mj_footer?.endRefreshing()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return data.count
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("\(indexPath[1])")
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
       // cell.textLabel?.text = String(data[indexPath.row])
        cell.labe.text = String(data[indexPath.row])
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
